﻿using AutoMapper.Internal.Mappers;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using ThridGroup.ERP.DTO.JobGradeManagementDTO;
using ThridGroup.ERP.DTO.MaterielDto;
using ThridGroup.ERP.HumanResources;
using ThridGroup.ERP.ProductionSystem;

namespace ThridGroup.ERP.Web.Controllers.ProductionSystem
{
    [Route("ProductionSystem/MasterData/[action]")]
    /// <summary>
    /// 主数据控制器
    /// </summary>
    public class MasterDataController : Controller
    {
        /// <summary>
        /// 物料信息接口
        /// </summary>
        private readonly IMaterielServices _materielServices;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="materielServices">物料信息接口</param>
        public MasterDataController(IMaterielServices materielServices)
        {
            _materielServices = materielServices;
        }

      

        /// <summary>
        /// 物料信息页面
        /// </summary>
        /// <returns></returns>
        public IActionResult MaterielView()
        {
            return View();
        }


       

        [HttpGet]
        /// <summary>
        /// 获取物料分类级联树形结构
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> GetMaterialTypeTree()
        {
            var roles = await _materielServices.GetMaterialTypeTree();
            return Json(new
            {
                code = 0,
                msg = "",
                count = roles.Count,
                data = roles
            });
        }

        [HttpPost]
        /// <summary>
        /// 添加物料信息
        /// </summary>
        /// <returns></returns>
        //public async Task<IActionResult> CreateMateriel([FromBody] CreateMaterielDto createMaterielDto)
        //{
        //    var result = await _materielServices.CreateMateriel(createMaterielDto);
        //    return Json(new
        //    {
        //        success = result > 0,
        //        message = result > 0 ? "添加成功" : "添加失败"
        //    });
        //}

        [HttpPost]
     
        public async Task<IActionResult> CreateMateriel([FromBody] CreateMaterielDto createMaterielDto)
        {
            try
            {
                var result = await _materielServices.CreateMateriel(createMaterielDto);
                return Json(new
                {
                    success = result > 0,
                    message = result > 0 ? "添加成功" : "添加失败"
                });
            }
            catch (Exception ex)
            {
                // 返回详细异常信息到前端（仅开发环境用）
                return Json(new { success = false, message = ex.ToString() });
            }
        }

        [HttpGet]
        /// <summary>
        /// 显示物联信息
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> QueryMateriel(GetMaterielDto getMaterielDto)
        {
            var list = await _materielServices.QueryMateriel(getMaterielDto);
            var jobGradeManagementModels = list.OrderByDescending(a => a.Id).Skip((getMaterielDto.PageIndex - 1) * getMaterielDto.PageSize).Take(getMaterielDto.PageSize).ToList();

            return Json(new
            {
                code = 0,
                msg = "",
                count = list.Count, // 如果有分页，这里应为总条数
                data = jobGradeManagementModels
            });
        }

        
        /// <summary>
        /// 修改物料信息
        /// </summary>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> UpdateMateriel([FromBody] CreateMaterielDto dto)
        {
            var result = await _materielServices.UpdateMateriel(dto);
            return Json(new
            {
                success = result > 0,
                message = result > 0 ? "修改成功" : "修改失败"
            });
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        /// <summary>
        /// 批量删除物料信息
        /// </summary>
        public async Task<IActionResult> BatchDeleteMateriel([FromBody] List<int> ids)
        {
            if (ids == null || !ids.Any())
                return Json(new { code = 1, msg = "未传递ID" });
            await _materielServices.BatchDeleteMateriel(ids);
            return Json(new { code = 0, msg = "删除成功" });
        }




    }
}
